package com.csnj.admin.utils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 导出多SheetExcel公共方法
 * @author xnby
 * @version 1.0
 */

interface DataFillHandler {
    List<String> getNeedFillData(int row);
}

public class ExportExcelHelper {
    /**
     * 导出数据
     */

    private static List<List<String>> genTitle(List<String> title) {
        List<List<String>> head = new ArrayList<List<String>>();
        for(String t: title) {
            List<String> arrs = new ArrayList<>();
            arrs.add(t);
            head.add(arrs);
        }
        return head;
    };

    public static void export(List<String> title, List<List<String>> data, HttpServletResponse response , String fileName) throws Exception {
        String fileNames = fileName + ".xlsx";
        response.resetBuffer();
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String( fileNames.getBytes("gb2312"), "ISO8859-1" ));

        ExcelWriter writer = EasyExcelFactory.getWriter(response.getOutputStream());
        Sheet sheet = new Sheet(1);
        sheet.setHead(genTitle(title));
        sheet.setAutoWidth(true);
        writer.write0(data, sheet);
        writer.finish();
    }

   }
